<template>
    <div class="users">
    <el-card class="box-card">
     <div class="top">
      <el-input    placeholder="请输入内容" v-model="getUserFn.query" class="input-with-select cc">
      <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
     </el-input>
      <el-button type="primary" @click="dialogAddVisible = true">添加用户</el-button>
     </div>
        <el-table
    :data="usersData"
    border
    style="width: 100%">
    <el-table-column
      label="#"
      type="index"
      width="100">
    </el-table-column>
    <el-table-column
      prop="username"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="email"
      label="邮箱"
      width="120">
    </el-table-column>
    <el-table-column
      prop="mobile"
      label="电话"
      width="120">
    </el-table-column>
    <el-table-column
      prop="role_name"
      label="角色"
      width="300">
    </el-table-column>
    <el-table-column
      label="状态"
      width="120">
      <template slot-scope="scope">
        <el-switch
      v-model="scope.row.mg_state"
      active-color="#13ce66"
      inactive-color="#ff4949"
      @change="change(scope.row.id,scope.row.mg_state)">
      </el-switch>
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="200">
      <template slot-scope="scope">
  <el-button type="primary" size="mini" icon="el-icon-edit" circle @click="edit(scope.row)"></el-button>
  <el-button type="warning" size="mini" icon="el-icon-setting" circle  @click="set(scope.row)" ></el-button>
  <el-button type="danger" size="mini" icon="el-icon-delete" circle @click="del(scope.row.id)"></el-button>
      </template>
    </el-table-column>
  </el-table>
  </el-card>
  <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="getUserFn.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="getUserFn.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
  <!-- 弹框 -->
  <el-dialog
  title="添加用户"
  :visible.sync="dialogAddVisible"
  width="30%"
  >
 <el-form :model="ruleForm5" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="ruleForm5.username"></el-input>
  </el-form-item>
   <el-form-item label="密码" prop="password">
    <el-input v-model="ruleForm5.password"></el-input>
  </el-form-item>
   <el-form-item label="手机号">
    <el-input v-model="ruleForm5.mobile"></el-input>
  </el-form-item>
   <el-form-item label="邮箱">
    <el-input v-model="ruleForm5.email"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="resetAddForm('ruleForm')">取 消</el-button>
    <el-button type="primary" @click="submitAddForm('ruleForm')">确 定</el-button>
  </span>
</el-dialog>
 <!-- 修改弹框 -->
  <el-dialog
  title="编辑用户"
  :visible.sync="dialogEditVisible"
  width="30%"
  >
 <el-form :model="ruleForm6" ref="ruleForm6" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名">
    <el-input v-model="ruleForm6.username" disabled></el-input>
  </el-form-item>
   <el-form-item label="手机号">
    <el-input v-model="ruleForm6.mobile"></el-input>
  </el-form-item>
   <el-form-item label="邮箱">
    <el-input v-model="ruleForm6.email"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogEditVisible=false">取 消</el-button>
    <el-button type="primary" @click="submitEditForm">确 定</el-button>
  </span>
</el-dialog>
 <!-- 分排角色 -->
  <el-dialog
  title="分排角色"
  :visible.sync="dialogRoleVisible"
  width="30%"
  >
  <p>当前的用户：{{username}}</p>
  <p>当前的角色：{{role_name}}</p>
  <p>分排的角色：  <el-select  v-model="select" slot="prepend" placeholder="请选择" @select="select">
      <el-option  v-for="item in myList" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
    </el-select></P>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogRoleVisible=false">取 消</el-button>
    <el-button type="primary" @click="setRole">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>

<script>
import { rolesM } from "@/http/roles.js";
import _ from "lodash"
import {
  getUsers,
  addUsers,
  del,
  xuUser,
  xuStatus,
  setRole
} from "@/http/user.js";
export default {
  data() {
    return {
      select: "",
      myList: [],
      username: "",
      role_name: "",
      dialogRoleVisible: false,
      index: 0,
      dialogEditVisible: false,
      ruleForm5: {
        username: "",
        password: "",
        mobile: "",
        email: ""
      },
      ruleForm6: {
        username: "",
        mobile: "",
        email: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      dialogAddVisible: false,
      total: 0,
      inputVal: "",
      usersData: [],
      getUserFn: {
        query: "",
        pagenum: 1,
        pagesize: 6
      }
    };
  },
  created() {
    this.getUsers();
  },
  // 处理分页bug
  watch: {
    usersData: {
      handler(n) {
        if (this.ruleForm5.pagenum > 1 && n.length == 0) {
          this.ruleForm5.pagenum -= 1;
          this.getUsers();
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    // 重置
    resetAddForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 添加用户
    submitAddForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let res = await addUsers(this.ruleForm5);
          if (res.meta.status == 201) {
            this.dialogAddVisible = false;
            this.getUsers();
          }
          this.ruleForm5 = {};
        }
      });
    },
    handleSizeChange(val) {
      this.getUserFn.pagesize = val;
      this.getUsers();
    },
    handleCurrentChange(val) {
      this.getUserFn.pagenum = val;
      this.getUsers();
    },
    // 获取用户信息
    async getUsers() {
      let res = await getUsers(this.getUserFn);
      // console.log(res);
      this.usersData = res.data.users;
      this.total = res.data.total;
      // console.log(this.usersData);
    },
    // 删除用户
    async del(id) {
      let res = await del(id);
      this.getUsers();
    },
    // 搜索框
    search: _.debounce(function() {
      console.log("hhh", this);
      this.ruleForm5.pagenum = 1;
      this.getUsers();
    }, 2000),
    // 回填
    edit(row) {
      this.index = row.id;
      this.dialogEditVisible = true;
      this.ruleForm6.username = row.username;
      this.ruleForm6.mobile = row.mobile;
      this.ruleForm6.email = row.email;
    },
    // 编辑用户
    async submitEditForm() {
      let res = await xuUser(this.index, {
        mobile: this.ruleForm6.mobile,
        email: this.ruleForm6.email
      });
      console.log(res);
      if (res.meta.status == 200) {
        this.dialogEditVisible = false;
        this.getUsers();
      }
    },
    // 改变状态
    async change(uid, type) {
      let res = await xuStatus(uid, type);
      if (res.meta.status == 200) {
        this.$message({
          message: "修改成功",
          type: "success"
        });
        this.getUsers();
      }
    },
    
    async set(row) {
      let res = await rolesM();
      console.log(res);
      this.myList = res.data;
      this.index = row.id;
      console.log(this.index); 
      this.dialogRoleVisible = true;
      this.username = row.username;
      this.role_name = row.role_name;
    },

    async setRole() {
      let res = await setRole(this.index, this.select);
      console.log("--", this.select); //--146
      console.log("--", this.index);   //500
      if (res.meta.status == 200) {
        this.dialogRoleVisible = false;
        this.getUsers();
        // alert("设置成功");
      }
    }
  }
};
</script>

<style lang="scss">
.top {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}
.cc {
  width: 500px;
  margin-right: 50px;
}
</style>
